<?php
/**
 * Created by PhpStorm.
 * User: Sixstar-Peter
 * Date: 2019/4/16
 * Time: 22:39
 * //一键协程化,默认不支持协程原生组件,支持协程
 */

/**
 * Created by PhpStorm.
 * User: Sixstar-Peter
 * Date: 2019/4/13
 * Time: 20:48
 */

Swoole\Runtime::enableCoroutine(true); //一键协程化

include "waitGroup.php";
$server = new  Swoole\Http\Server('0.0.0.0', 9800);
$server->on('request', function ($req, $response) {
    if ($req->server['request_uri'] == '/favicon.ico') {
        return;
    }
    $wait = new waitGroup();
    $wait->add(); //每启动一个协程+1
    go(function () use ($wait) {
        //$swoole_mysql = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'Peter123!');
        //$swoole_mysql->query('select sleep(3)');
        sleep(3);
        $wait->push(['data' => 1]);
    });
    $wait->add();
    go(function () use ($wait) {
        //$swoole_mysql = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'Peter123!');
        //$swoole_mysql->query('select sleep(3)');
        sleep(2);
        $wait->push(['data' => 2]);
    });
    $result = $wait->wait(); //阻塞接收协程返回的结果
    $response->end(json_encode($result));
});

$server->start();